/*
* LibraryController.java
* Created on  2014-7-30 下午2:30
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-7-30       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.classroom;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classroom.entity.ClassroomInfo;
import com.genghis.ptas.classroom.service.ClassroomService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.system.entity.DictValues;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.LoginUserUtil;
import com.genghis.ptas.util.TaskFileUp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author gaoxinyu
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/classroom/")
public class ClassroomController {
    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private DictService dictService;

    @Autowired
    private UserService userService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "listAllClassroomDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllClassroomDo() {
        return "backstage/classroom/listAllClassroomDo";
    }

    @RequestMapping(value = "listAllClassroom", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllClassroom(PageParameter pageParameter, ClassroomInfo classroomInfo) {
        return classroomService.findClassroomInfoPageList(pageParameter, classroomInfo);
    }

    @RequestMapping(value = "addClassroomDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String addDictDefineDo(HttpServletRequest request) throws ServletException, IOException {
        request.setAttribute("content", TaskFileUp.file(request, "upFile"));
        List<DictValues> dictValuesList = dictService.findAllDictValues(DictConstant.TASKTYPE);
        request.setAttribute("type", dictValuesList);
        return "backstage/classroom/addClassroomDo";
    }

    @RequestMapping(value = "addClassroomInfo", method = RequestMethod.POST)
    @ResponseBody
    public void addClassroomInfo(ClassroomInfo classroomInfo) {
        classroomService.addClassroomInfo(classroomInfo);
    }

    @RequestMapping(value = "editClassroomInfoDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editClassroomInfoDo(HttpServletRequest request, Integer id) {
        ClassroomInfo classroomInfo = classroomService.findClassroomInfoById(id);
        request.setAttribute("classroomInfo", classroomInfo);
        return "backstage/classroom/editClassroomDo";
    }

    @RequestMapping(value = "deleteClassroomInfo", method = RequestMethod.DELETE)
    @ResponseBody
    public void deleteClassroomInfo(ClassroomInfo classroomInfo) {
        classroomService.deleteClassroomCourseByClassroomId(classroomInfo.getId());
        classroomService.deleteClassroomInfoById(classroomInfo.getId());
    }

    @RequestMapping(value = "editClassroomInfo", method = RequestMethod.POST)
    @ResponseBody
    public void editClassroomInfo(ClassroomInfo classroomInfo) {
        classroomService.editClassroomInfo(classroomInfo);
    }

    @RequestMapping(value = "userJoinClassroom", method = RequestMethod.POST)
    @ResponseBody
    public Boolean userJoinClassroom(Integer classroomId) {
        boolean judge = classroomService.judgeReapedClassroomStudent(classroomId);
        if (judge){
            classroomService.addClassroomStudent(classroomId);
        }
        return judge;
    }
}
